package com.google.gwt.sample.dynatablerf.client.views;

import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.sample.dynatablerf.client.presenters.ProjetoPresenter.ProjetoDisplay;
import com.google.gwt.sample.dynatablerf.client.presenters.ProjetoPresenter.ValorStatus;
import com.google.gwt.sample.dynatablerf.client.theme.GrifoTheme;
import com.google.gwt.sample.dynatablerf.client.theme.GrifoTheme.GrifoImageResources;
import com.google.gwt.sample.dynatablerf.client.util.GrifoConstants;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TabLayoutPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class ProjetoView extends Composite implements ProjetoDisplay {

	private TabLayoutPanel projetoTabLayoutPanel;
	private Image closeTabImage;
	private String numero;
	private Label fatorCOMValue;
	private Label fatorCAValue;
	private VerticalPanel imageCOMPanel;
	private VerticalPanel imageCAPanel;
	private Image imageCOMValue;
	private Image imageCAValue;

	public ProjetoView() {

		DockPanel dockPanel = new DockPanel();
		dockPanel.setSize("100%", "100%");
		initWidget(dockPanel);

		HorizontalPanel horizontalPanel = GrifoTheme.getFatorPanel();

		Label fatorCOMLabel = new Label(GrifoConstants.INSTANCE.fatorCOM());
		horizontalPanel.add(fatorCOMLabel);

		imageCOMPanel = new VerticalPanel();
		horizontalPanel.add(imageCOMPanel);

		fatorCOMValue = new Label();

		horizontalPanel.add(fatorCOMValue);

		Label fatorCALabel = new Label(GrifoConstants.INSTANCE.fatorCA());
		horizontalPanel.add(fatorCALabel);

		imageCAPanel = new VerticalPanel();
		horizontalPanel.add(imageCAPanel);

		fatorCAValue = new Label();
		horizontalPanel.add(fatorCAValue);

		dockPanel.add(horizontalPanel, DockPanel.NORTH);

		projetoTabLayoutPanel = GrifoTheme.getTabLayoutPanel();
		projetoTabLayoutPanel.setSize("100%", "300px");

		dockPanel.add(projetoTabLayoutPanel, DockPanel.CENTER);

		closeTabImage = GrifoTheme.getCloseTabImage();

	}

	@Override
	public HasClickHandlers getCloseTabClickHandlers() {
		return closeTabImage;
	}

	@Override
	public Widget getTabHeader() {
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		Label label = GrifoTheme.getLabelPadrao();
		label.setText(numero);
		horizontalPanel.add(label);
		horizontalPanel.add(closeTabImage);
		return horizontalPanel;
	}

	@Override
	public TabLayoutPanel getTabLayoutPanel() {
		return projetoTabLayoutPanel;
	}

	@Override
	public void setNumero(String numero) {
		this.numero = numero;

	}

	@Override
	public void setValorCOM(String valorCOM) {
		fatorCOMValue.setText(valorCOM);

	}

	@Override
	public void setValorCA(String valorCA) {
		fatorCAValue.setText(valorCA);

	}

	@Override
	public void updateValorCAIcon(ValorStatus valorStatus) {
		if (valorStatus == ValorStatus.NENHUM)
			imageCAPanel.setVisible(false);
		else {
			imageCAPanel.setVisible(true);

			if (imageCAValue != null) {
				imageCAPanel.remove(imageCAValue);
			}
			if (valorStatus == ValorStatus.IGUAL)
				imageCAValue = new Image(
						GrifoImageResources.INSTANCE.iconQuadradoVermelho());
			else if (valorStatus == ValorStatus.AUMENTO)
				imageCAValue = new Image(
						GrifoImageResources.INSTANCE.iconSetaVermelha());
			else
				imageCAValue = new Image(
						GrifoImageResources.INSTANCE.iconSetaVerde());
			imageCAPanel.add(imageCAValue);
		}

	}

	@Override
	public void updateValorCOMIcon(ValorStatus valorStatus) {
		if (valorStatus == ValorStatus.NENHUM)
			imageCOMPanel.setVisible(false);
		else {
			imageCOMPanel.setVisible(true);

			if (imageCOMValue != null) {
				imageCOMPanel.remove(imageCOMValue);
			}
			if (valorStatus == ValorStatus.IGUAL)
				imageCOMValue = new Image(
						GrifoImageResources.INSTANCE.iconQuadradoVermelho());
			else if (valorStatus == ValorStatus.AUMENTO)
				imageCOMValue = new Image(
						GrifoImageResources.INSTANCE.iconSetaVermelha());
			else
				imageCOMValue = new Image(
						GrifoImageResources.INSTANCE.iconSetaVerde());
			imageCOMPanel.add(imageCOMValue);
		}

	}

}
